package com.tsmti.doctor.controller;

import com.tsmti.basicdata.entity.WardInformation;
import com.tsmti.core.util.StringUtils;
import com.tsmti.system.entity.Menu;
import com.tsmti.util.UserUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
 * @Package：com.tsmti.doctor.controller
 * @Class：IndexController
 * @Description： TODO
 * @Author：king
 * @Date：Created in 2018/7/2 15:42
 * @Company:
 * @Version：
 * @Modified By:
 */
@Controller
public class IndexController {
    @RequestMapping(value = "/index" , method = RequestMethod.GET)
    public String index(Model model, HttpServletRequest request, HttpServletResponse response) {
        // 加载菜单
        model.addAttribute("menus", UserUtils.getMenuList());
        // 加载模版
//		String theme = getTheme(request);
        return "home";
    }

    /**
     * 组装参数
     * @param o 实体类
     * @return
     */
    public String reflect(Object o){
        StringBuffer res = new StringBuffer();
        //获取参数类
        Class cls = o.getClass();
        //将参数类转换为对应属性数量的Field类型数组（即该类有多少个属性字段 N 转换后的数组长度即为 N）
        Field[] fields = cls.getDeclaredFields();
        for(int i = 0;i < fields.length; i ++){
            Field f = fields[i];
            f.setAccessible(true);
            try {
                Object oo = f.get(o);
                String propertyTypeName = String.valueOf(f.getGenericType()).replace("class","").trim();
                String value = "";
                if(oo != null){
                    if (propertyTypeName.equals(int.class.getName())
                            ||propertyTypeName.equals(Integer.class.getName())
                            ||propertyTypeName.equals(long.class.getName())
                            ||propertyTypeName.equals(Long.class.getName())
                            ||propertyTypeName.equals(double.class.getName())
                            ||propertyTypeName.equals(Double.class.getName())
                            ||propertyTypeName.equals(float.class.getName())
                            ||propertyTypeName.equals(Float.class.getName())
                            ||propertyTypeName.equals(byte.class.getName())
                            ||propertyTypeName.equals(Byte.class.getName())
                            ||propertyTypeName.equals(short.class.getName())
                            ||propertyTypeName.equals(Short.class.getName())
                            ||propertyTypeName.equals(char[].class.getName())
                            ||propertyTypeName.equals(Character[].class.getName())
                            ||propertyTypeName.equals(boolean.class.getName())
                            ||propertyTypeName.equals(Boolean.class.getName())
                            ||propertyTypeName.equals(String.class.getName())) {
                        value = String.valueOf(oo);
                    } else if (propertyTypeName.equals(java.util.Date.class.getName())) {
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        value = sdf.format(oo);
                    }
                }
                if(StringUtils.isNotEmpty(value)){
                    if(StringUtils.isEmpty(res.toString())){
                        res.append(f.getName() + "=" + value);
                    }else{
                        res.append("&" + f.getName() + "=" + value);
                    }
                }
            } catch (IllegalArgumentException | IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return res.toString();
    }
}
